import * as THREE from "three";
import { DEG2RAD } from "three/src/math/MathUtils.js";
import { City } from "../../City";
import { Building } from "../Building";
import { Development } from "../modules/Development";
import { assetManager } from "../../../AssetManager";

/**
 * 区域
 */
export class Zone extends Building {

    style = ['A', 'B', 'C'][Math.floor(3 * Math.random())];

    development = new Development(this);

    constructor() {

        super();

        this.name = 'Zone';
        this.power.required = 10;
        this.rotation.y = 90 * Math.floor(4 * Math.random()) * DEG2RAD;

    }

    simulate(city: City): void {
        super.simulate(city);
        this.development.simulate();
    }

    refreshView(city?: City) {

        let modelName: string;
        switch (this.development.state) {
            case Development.state.underConstruction:
            case Development.state.undeveloped:
                modelName = 'under-construction';
                break;
            default:
                modelName = `${this.type}-${this.style}${this.development.level}`;
                break;
        }

        const model = assetManager.getModel(modelName, this);

        // Tint building a dark color if it is abandoned
        if (this.development.state === Development.state.abandoned) {
            model.traverse((obj) => {
                if (obj instanceof THREE.Mesh) {
                    obj.material.color = new THREE.Color(0x707070);
                }
            });
        }

        this.setMesh(model);
    }

    toHTML(): string {

        let html = super.toHTML();
        html += this.development.toHTML();
        return html;

    }
}